﻿Public Class frmEmail

    Private mvarChanged As Boolean
    Private mvarCancel As Boolean = False
    Private mvarDbHolder As clsDataBase
    Private mvarKlientId As Integer
    Private mvarEmail As New clsEmail
    Private mvarEmailId As Integer
    Private mvarUzytkownik As clsUzytkownik
    Private mvarAdresEmail As String

    Private mvarOferta As clsSzukanieOferty
    Private mvarSzukanieOferty As clsSzukanieOferty


    Public Sub New(ByVal IdKlienta As Integer, ByVal Nazwaklienta As String)

        ' To wywołanie jest wymagane przez projektanta.
        InitializeComponent()

        ' Dodaj inicjację po wywołaniu funkcji InitializeComponent().
        mvarDbHolder = New clsDataBase(VAR_CONNECTION_PARAMETERS, VAR_ERROR_HANDLER)
        mvarUzytkownik = clsUzytkownik.Instance()
        mvarKlientId = IdKlienta
        txtKlientNazwa.Text = Nazwaklienta

        uzupelnienie(IdKlienta)
    End Sub



    Private Sub uzupelnienie(ByVal IdKlienta As Integer)
        Dim lvarDataTable As New DataTable
        lvarDataTable = clsEmail.PobierzWszystkieEmail(mvarDbHolder, IdKlienta, VAR_ERROR_HANDLER)
        If Not lvarDataTable Is Nothing Then
            For Each lvarRow As DataRow In lvarDataTable.Rows
                dgvEmail.Rows.Add(lvarRow("id"), lvarRow("klient_id"), lvarRow("email"))
            Next
            If dgvEmail.Rows.Count > 0 Then
                dgvEmail.ClearSelection()
            End If
          
            End If
    End Sub

    Private Sub cmdDodaj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDodaj.Click

        mvarEmailId = -1
        Zapisz()
        dgvEmail.Rows.Clear()
        uzupelnienie(mvarKlientId)
        txtDodajEmail.Clear()
    End Sub

    Private Sub cmbEdytujEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbEdytujEmail.Click
        If dgvEmail.SelectedRows.Count = 0 Then
            MessageBox.Show("Wybierz ofertę.", "WIREMET")
        Else
            Zapisz()
            dgvEmail.Rows.Clear()
            uzupelnienie(mvarKlientId)
        End If
        txtDodajEmail.Clear()

    End Sub

    Private Sub dgvEmail_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvEmail.CellMouseClick

        If dgvEmail.SelectedRows.Count > 0 Then
            txtDodajEmail.Text = dgvEmail.SelectedRows.Item(0).Cells("ColEmail").Value.ToString()
            mvarEmailId = CInt(dgvEmail.SelectedRows.Item(0).Cells("ColIdEmail").Value)
        End If
    End Sub

    Private Sub Zapisz()
        mvarDbHolder.BeginTrans()
        With mvarEmail
            .Id = mvarEmailId
            .KlientId = mvarKlientId
            .Email = txtDodajEmail.Text
            .Save(mvarDbHolder, VAR_ERROR_HANDLER)
        End With
        mvarDbHolder.CommitTrans()
        ' MessageBox.Show("Zapisano adres email.", "WIREMET")
    End Sub

    Private Sub cmdUsun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUsun.Click
        If dgvEmail.SelectedRows.Count > 0 Then
            Dim lvarCommand As String
            Dim lvarCommand1 As String
            lvarCommand = " oferta.email_id='" & mvarEmailId & "'"
            lvarCommand1 = ""
            mvarSzukanieOferty = New clsSzukanieOferty
            mvarSzukanieOferty.Search(mvarDbHolder, VAR_ERROR_HANDLER, lvarCommand, lvarCommand1)
            If mvarSzukanieOferty.Oferty.Rows.Count > 0 Then
                MessageBox.Show("Adres email został użyty, nie można usunąć.", "WIREMET")
            Else
                mvarEmailId = CInt(dgvEmail.SelectedRows.Item(0).Cells("ColIdEmail").Value)

                mvarDbHolder.BeginTrans()
                With mvarEmail
                    .Id = mvarEmailId
                    .Delete(mvarDbHolder, VAR_ERROR_HANDLER)
                End With
                mvarDbHolder.CommitTrans()
                'MessageBox.Show("Zapisano adres email.", "WIREMET")
                dgvEmail.Rows.Clear()
                uzupelnienie(mvarKlientId)
                txtDodajEmail.Clear()
            End If
        Else
            MessageBox.Show("Wybierz adres email aby usunąć.", "WIREMET")
        End If
    End Sub

End Class